package org.ourgrid.worker.business.requester;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.common.interfaces.to.IncomingHandle;
import org.ourgrid.common.interfaces.to.WorkAccounting;
import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.RequesterIF;
import org.ourgrid.common.internal.response.LoggerResponseTO;
import org.ourgrid.common.util.FileTransferHandlerUtils;
import org.ourgrid.worker.business.controller.FileTransferController;
import org.ourgrid.worker.business.dao.WorkerDAOFactory;
import org.ourgrid.worker.business.dao.WorkerStatusDAO;
import org.ourgrid.worker.business.messages.WorkerControllerMessages;
import org.ourgrid.worker.request.TransferRequestReceivedRequestTO;

public class TransferRequestReceivedRequester implements RequesterIF<TransferRequestReceivedRequestTO> {

	public List<IResponseTO> execute(TransferRequestReceivedRequestTO request) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		IncomingHandle handle = request.getHandle();
		String destinationFile = parseDestinationFile(handle.getDescription());
		
		WorkerStatusDAO workerStatusDAO = WorkerDAOFactory.getInstance().getWorkerStatusDAO();
		String consumerServiceID = workerStatusDAO.getConsumerAddress();
		
		if (consumerServiceID == null) {
			FileTransferController.getInstance().rejectTransferRequest(handle, responses);
			responses.add(new LoggerResponseTO(WorkerControllerMessages.
					getUnknownClientRequestsToTransferFileMessage(destinationFile,
							handle.getFileSize(), handle.getId(), handle.getSenderContainerID().toString()), LoggerResponseTO.WARN));
			
			return responses;
		}
		
		String consumerContainerID = getContainerID(consumerServiceID);
		
		if (!consumerContainerID.equals(handle.getSenderContainerID())) {
			responses.add(new LoggerResponseTO(WorkerControllerMessages.
					getUnknownClientRequestsToTransferFileMessage(destinationFile,
							handle.getFileSize(), handle.getId(), handle.getSenderContainerID().toString()), LoggerResponseTO.WARN));
			FileTransferController.getInstance().rejectTransferRequest(handle, responses);
			
			return responses;
		}
			
		String senderPublicKey = request.getConsumerPublicKey();
		
		if(!workerStatusDAO.hasConsumer()) {
			responses.add(new LoggerResponseTO(WorkerControllerMessages.
					getClientTriesToTransferFileOnUnstartedWorkerMessage(senderPublicKey), LoggerResponseTO.WARN));
			FileTransferController.getInstance().rejectTransferRequest(handle, responses);
			
			return responses;
		}
		
		if(workerStatusDAO.isFileTransferErrorState()) {
			responses.add(new LoggerResponseTO(WorkerControllerMessages.
					getClientRequestsToTransferFileOnWorkerWithErrorMessage(destinationFile,
							handle.getFileSize(), handle.getId(), senderPublicKey), LoggerResponseTO.WARN));

			return responses;
		}
		
		handle.setSenderPublicKey(workerStatusDAO.getConsumerPublicKey());
		
		WorkAccounting actualWorkAccounting = WorkerDAOFactory.getInstance().
									getWorkAccountingDAO().getActualWorkAccounting();
		
		if (actualWorkAccounting != null) {
			actualWorkAccounting.incDataTransfered(handle.getFileSize());
		}
		
		FileTransferController.getInstance().acceptTransferRequest(handle, destinationFile, responses);
		
		return responses;
	}
	
	private static String parseDestinationFile(String description) {
		return FileTransferHandlerUtils.getDestinationFile(description);
	}
	
	private String getContainerID( String serviceID ) {
		String[ ] split = serviceID.split( "/" );
		return split[0] + "/" +split[1];
	}
}
